package com.example.liteflow.constant;

/**
 * 2025/9/6.
 *
 * @author haijin
 */
public interface DynamicConstant {

    /**
     * 声明式事务,不保存数据，正常
     */
    String TEST_1 = """
            import com.yomahub.liteflow.core.NodeComponent;
                        import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
                        import com.example.liteflow.entity.User;
                        import com.example.liteflow.service.UserService;
                        import org.springframework.transaction.annotation.Transactional;
                         
                        public class Demo extends NodeComponent {
                        
                            @Transactional(rollbackFor = Exception.class)
                            @Override
                            public void process() throws Exception {
                                System.out.println("Hello");
                            }
                        }
            """;
    /**
     * 声明式事务,不保存数据，异常
     */
    String TEST_1_3 = """
            import com.yomahub.liteflow.core.NodeComponent;
                        import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
                        import com.example.liteflow.entity.User;
                        import com.example.liteflow.service.UserService;
                        import org.springframework.transaction.annotation.Transactional;
                         
                        public class Demo extends NodeComponent {
                        
                            @Transactional(rollbackFor = Exception.class)
                            @Override
                            public void process() throws Exception {
                                System.out.println("Hello");
                                int i = 1/0;
                            }
                        }
            """;
    /**
     * 声明式事务,保存数据，正常
     */
    String TEST_1_1 = """
            import com.yomahub.liteflow.core.NodeComponent;
                        import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
                        import com.example.liteflow.entity.User;
                        import com.example.liteflow.service.UserService;
                        import org.springframework.transaction.annotation.Transactional;
                         
                        public class Demo extends NodeComponent {
                        
                            @Transactional(rollbackFor = Exception.class)
                            @Override
                            public void process() throws Exception {
                                UserService userService = ContextAwareHolder.loadContextAware().getBean(UserService.class);
                                User user = new User();
                                user.setName("test");
                                user.setAddress("广州");
                       
                                userService.save(user);
                                System.out.println("Hello");
                            }
                        }
            """;

    /**
     * 不加声明式事务,保存数据，正常
     */
    String TEST_1_1_1 = """
            import com.yomahub.liteflow.core.NodeComponent;
                        import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
                        import com.example.liteflow.entity.User;
                        import com.example.liteflow.service.UserService;
                         
                        public class Demo extends NodeComponent {

                            @Override
                            public void process() throws Exception {
                                UserService userService = ContextAwareHolder.loadContextAware().getBean(UserService.class);
                                User user = new User();
                                user.setName("test");
                                user.setAddress("广州");
                       
                                userService.save(user);
                                System.out.println("Hello");
                            }
                        }
            """;

    /**
     * 不加声明式事务,保存数据，正常
     */
    String TEST_1_1_1_1 = """
            import com.yomahub.liteflow.core.NodeComponent;
                        import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
                        import com.example.liteflow.entity.User;
                        import com.example.liteflow.mapper.UserMapper;
                         
                        public class Demo extends NodeComponent {

                            @Override
                            public void process() throws Exception {
                                UserMapper userMapper = ContextAwareHolder.loadContextAware().getBean(UserMapper.class);
                                User user = new User();
                                user.setName("test");
                                user.setAddress("广州");
                       
                                userMapper.insert(user);
                                System.out.println("插入数据");
                            }
                        }
            """;

    /**
     * 声明式事务,保存数据，抛出异常
     */
    String TEST_2 = """
            import com.yomahub.liteflow.core.NodeComponent;
                        import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
                        import com.example.liteflow.entity.User;
                        import com.example.liteflow.service.UserService;
                        import org.springframework.transaction.annotation.Transactional;
                         
                        public class Demo extends NodeComponent  {
                        
                            @Transactional(rollbackFor = Exception.class)
                            @Override
                            public void process() throws Exception {
                                UserService userService = ContextAwareHolder.loadContextAware().getBean(UserService.class);
                                User user = new User();
                                user.setName("test");
                                user.setAddress("广州");
                       
                                userService.save(user);
                                int i = 1/0;
                                System.out.println("Hello");
                            }
                        }
            """;

    /**
     * 编程式事务,保存数据，正常
     */
    String TEST_3 = """
            import com.yomahub.liteflow.core.NodeComponent;
             import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
             import com.example.liteflow.entity.User;
             import com.example.liteflow.service.UserService;
             import org.springframework.transaction.support.TransactionTemplate;
             import org.springframework.transaction.TransactionStatus;
             import org.springframework.transaction.support.DefaultTransactionDefinition;
             
             public class Demo extends NodeComponent {
             
                 @Override
                 public void process() throws Exception {
                     // 获取 UserService 和 TransactionTemplate
                     UserService userService = ContextAwareHolder.loadContextAware().getBean(UserService.class);
                     TransactionTemplate transactionTemplate = ContextAwareHolder.loadContextAware().getBean(TransactionTemplate.class);
                    
                     // 使用编程式事务执行操作
                     transactionTemplate.execute(status -> {
                         try {
                             User user = new User();
                             user.setName("test");
                             user.setAddress("广州");
                            
                             userService.save(user);
//                             int i = 1/0; // 这里会抛出异常，触发回滚
                             System.out.println("Hello");
                            
                             return null; // 执行成功返回null
                         } catch (Exception e) {
                             // 设置回滚标志
                             status.setRollbackOnly();
                             throw new RuntimeException("事务执行失败，已回滚", e);
                         }
                     });
                 }
             }
            """;

    /**
     * 编程式事务,保存数据，异常
     */
    String TEST_3_1 = """
            import com.yomahub.liteflow.core.NodeComponent;
             import com.yomahub.liteflow.spi.holder.ContextAwareHolder;
             import com.example.liteflow.entity.User;
             import com.example.liteflow.service.UserService;
             import org.springframework.transaction.support.TransactionTemplate;
             import org.springframework.transaction.TransactionStatus;
             import org.springframework.transaction.support.DefaultTransactionDefinition;
             
             public class Demo extends NodeComponent {
             
                 @Override
                 public void process() throws Exception {
                     // 获取 UserService 和 TransactionTemplate
                     UserService userService = ContextAwareHolder.loadContextAware().getBean(UserService.class);
                     TransactionTemplate transactionTemplate = ContextAwareHolder.loadContextAware().getBean(TransactionTemplate.class);
                    
                     // 使用编程式事务执行操作
                     transactionTemplate.execute(status -> {
                         try {
                             User user = new User();
                             user.setName("test");
                             user.setAddress("广州");
                            
                             userService.save(user);
                             int i = 1/0; // 这里会抛出异常，触发回滚
                             System.out.println("Hello");
                            
                             return null; // 执行成功返回null
                         } catch (Exception e) {
                             // 设置回滚标志
                             status.setRollbackOnly();
                             throw new RuntimeException("事务执行失败，已回滚", e);
                         }
                     });
                 }
             }
            """;


}
